import Taro from "@tarojs/taro";
import React, { useCallback } from "react";
import ListItem, { ListItemWhenLoggedIn } from "./index";

type NavigateItemProps = Omit<React.ComponentProps<typeof ListItem>, 'onPress'> & {
  url: string;
  needLoggedIn?: boolean;
}

const NavigateItem: React.FC<NavigateItemProps> = React.memo(({ children, url, needLoggedIn = true, onPress, ...props }) => {
  const handlePress = useCallback(() => {
    if (onPress) {
      onPress();
    } else {
      Taro.navigateTo({ url });
    }
  }, [url, onPress]);

  const Item = needLoggedIn ? ListItemWhenLoggedIn : ListItem;

  return (
    <Item {...props} onPress={handlePress}>{children}</Item>
  );
});

export default NavigateItem;
